package com.briup.pts.service.impl;

import com.briup.pts.config.exception.ServiceException;
import com.briup.pts.domain.bean.QuarantineRegistration;
import com.briup.pts.mapper.QrMapper;
import com.briup.pts.service.QrService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class QrServiceImpl implements QrService {

    private final QrMapper mapper;


    @Override
    public PageInfo<QuarantineRegistration> pageQueryByCondition(Integer pageNum, Integer pageSize,
                                                                 String bQualified, String grMechanism) {
        // 通过pageHelper进行分页,开始分页
        //PageHelper完成分页后,需要关闭资源
        try (Page<QuarantineRegistration> ignored = PageHelper.startPage(pageNum,pageSize)){
            // 根据条件查询数据
            List<QuarantineRegistration> list
                    = mapper.queryCondition(bQualified,grMechanism);

            // 将数据放入PageInfo中返回
            return new PageInfo<>(list);
        }
    }

    @Override
    public void saveOrUpdate(QuarantineRegistration quarantineRegistration) {
        //根据质检报告id是否存在,决定是新增还是修改
        if (quarantineRegistration.getGrId()==null){
            // id不存在,表示新增质检数据
            mapper.insertQuarantineRegistration(quarantineRegistration);
        }else {
            // id存在,表示修改
            mapper.modifyQuarantineRegistration(quarantineRegistration);
        }
    }


    @Override
    public void deleteById(Integer grId) {
        mapper.deleteById(grId);
    }

    @Override
    public void deleteByIdAll(List<Integer> grIds) {
        if (CollectionUtils.isEmpty(grIds)) {
            throw new ServiceException("没有需要删除的数据");
        }
        mapper.deleteByIdAll(grIds);
    }
}
